package com.hoshiicloud.saleorder.controller.customer;

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.saleorder.param.customer.ReviewsAddParam;
import com.hoshiicloud.saleorder.param.customer.ReviewsQueryParam;
import com.hoshiicloud.saleorder.param.customer.ReviewsUpdateParam;
import com.hoshiicloud.saleorder.service.customer.IReviewsService;
import com.hoshiicloud.user.entity.customer.Reviews;
import com.hoshiicloud.user.vo.customer.ReviewsDetailVO;
import com.hoshiicloud.user.vo.customer.ReviewsVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 平台商品评价 前端控制器
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Api(value = "前台用户操作", tags = { "平台商品评价管理" })
@Slf4j
@Validated
@RestController
@RequestMapping("/customer/reviews")
public class ReviewsController {
	@Autowired
	private IReviewsService reviewsService;

	@ApiOperation(value = "新增平台商品评价", notes = "")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response add(@RequestBody @Valid ReviewsAddParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		Reviews entity = new Reviews();
		BeanUtils.copyProperties(param, entity);
		// TODO 只接受指定参数
		return reviewsService.insert(entity);
	}

	@ApiOperation(value = "修改平台商品评价", notes = "初始评价在时限内可以修改一次,追加评价不可修改")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response update(@RequestBody @Valid ReviewsUpdateParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		Reviews entity = new Reviews();
		BeanUtils.copyProperties(param, entity);
		// TODO 只接受指定参数
		return reviewsService.updateDataById(entity);
	}

	@ApiOperation(value = "删除平台商品评价", notes = "")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Response delete(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return reviewsService.deleteDataById(id);
	}

	@ApiOperation(value = "获取平台商品评价详情", notes = "如果有追评，一并返回")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<ReviewsDetailVO> getDetail(@RequestParam String id) throws Exception {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return reviewsService.getDetail(id);
	}

	@ApiOperation(value = "获取平台商品评价列表", notes = "和追评一并返回")
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	public Response<List<ReviewsVO>> getList(@RequestBody @Valid ReviewsQueryParam param, BindingResult bindingResult)
			throws Exception {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		return reviewsService.getList(param);
	}

	@ApiOperation(value = "分页查询平台商品评价", notes = "和追评一并返回")
	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	public Response<IPage<ReviewsVO>> getPage(@RequestBody @Valid ReviewsQueryParam param, BindingResult bindingResult)
			throws Exception {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		IPage<Reviews> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));

		return reviewsService.getPage(page, param);
	}
}
